package com.lagou.controller;

import com.lagou.WebdemoApplication;
import com.lagou.bean.ConsumerComponent;
import com.lagou.common.Common;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author banzijian
 * @date 2020/10/26
 **/
@RestController
public class IndexController {
    @Resource
    private ConsumerComponent service;

    @RequestMapping("/")
    public String index(HttpServletRequest request){
        // 获取用户真实ip
        String ip = "";
        String forwardedFor = request.getHeader("x-forwarded-for");
        if (!StringUtils.isEmpty(forwardedFor)) {
            ip = forwardedFor.split(",")[0];
        } else if (!StringUtils.isEmpty(request.getHeader("X-Real-IP"))) {
            ip = request.getHeader("X-Real-IP");
        } else {
            ip = request.getRemoteAddr();
        }
        System.out.println("controller 接收到请求, ip: " + ip);
        // 放入threadLocal
        Common.IP_THREAD_LOCAL.set(ip);
        // 调用两个远程服务
        String hello = service.sayHello("lagou");
        String hi = service.sayHi("ban");
        // 返回数据给前端
        return hello + "</br>" + hi;
    }
}
